/*
********************************************************************************
*
*	file : mian.c
*
********************************************************************************
*/
#include "ff.h"

#include "usb_task.h"
#include "framework.h"
#include "sys_file_cfg.h"

#define file usb_man.fil

/*******************************************************************************
 * @brief  var
 *******************************************************************************/
static uint16_t bytesWritten, bytesToWrite;
static FRESULT res;

/*******************************************************************************
 * @brief  file_open :
 * \param[in] none
 * \retval: 0-err, 1-ok
 *******************************************************************************/
uint16_t sys_file_open(const char *file_name, uint8_t mode)
{
	uint8_t st;

	st = 0;

	switch (mode)
	{
	case FIL_OPEN_R:
		if (f_open(&file, file_name, FA_READ) == FR_OK)
			st = 1;
		break;
	case FIL_OPEN_W:
		if (f_open(&file, file_name, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK)
			st = 1;
		break;
	case FIL_OPEN_WA:
		if (f_open(&file, file_name, FA_WRITE | FA_CREATE_ALWAYS) == FR_OK)
			st = 1;
		break;
	default:
		st = 0;
		break;
	}

	return st;
}

/*******************************************************************************
 * @brief  sys_file_read :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
uint16_t sys_file_read(void *buff, uint16_t len)
{
	uint32_t len_back = 0;

	if (f_read(&file, buff, len, &len_back) != FR_OK)
	{
		len_back = 0;
	}

	return len_back;
}

/*******************************************************************************
 * @brief  sys_file_write :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
uint16_t sys_file_write(void *buff, uint16_t len)
{
	uint32_t len_back = 0;

	if (f_write(&file, buff, len, &len_back) != FR_OK)
	{
		len_back = 0;
	}

	return len_back;
}

/*******************************************************************************
 * @brief  sys_file_seek:
 * \param[in] none
 * \retval: none
 *******************************************************************************/
uint32_t sys_file_seek(uint32_t site)
{
	if (f_lseek(&file, site) != FR_OK)
	{
		return 0;
	}

	return file.obj.objsize;
}

/*******************************************************************************
 * @brief  hist_file_seek_end :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
uint32_t sys_file_size_get(void)
{
	return file.obj.objsize;
}

/*******************************************************************************
 * @brief  file_open :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
uint16_t sys_file_close(void)
{
	f_close(&file);

	return 1;
}
/*******************************************************************************
 * @brief  main : std proc entry.
 * \param[in] none
 * \retval: none
 *******************************************************************************/
uint16_t sys_file_dir_open(const char *path)
{
	DIR *wk_dir;
	FRESULT st;

	wk_dir = malloc(sizeof(DIR));

	st = f_opendir(wk_dir, path);

	if (st != FR_OK)
	{
		f_mkdir(path);

		st = f_opendir(wk_dir, path);
	}

	f_closedir(wk_dir);

	f_chdir(path);

	free(wk_dir);

	return st;
}
